define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'car_model/index' + location.search,
                    add_url: 'car_model/add',
                    edit_url: 'car_model/edit',
                    del_url: 'car_model/del',
                    multi_url: 'car_model/multi',
                    import_url: 'car_model/import',
                    table: 'car_model',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedRightNumber: 1,
                searchFormVisible: true,// 默认显示搜索条件
                search: false,// 搜索框
                commonSearch: false,// 搜索按钮
                showToggle: false,// 切换视图
                showColumns: false,// 显示列
                showExport: false,// 导出按钮
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'series_id', title: __('Series_id')},
                        {field: 'brand.name', title: __('品牌'), operate: 'LIKE'},
                        {field: 'series.name', title: __('Series.name'), operate: 'LIKE'},

                        {field: 'name', title: __('车型'), operate: 'LIKE'},
                        
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                // 监听品牌下拉框的选中值，并发送ajax请求,改变车型下拉框内容
                $("#c-brand_id").on("change", function () {
                    var brand_id = $("#c-brand_id").val();
                    // console.log(brand_id);
                    // ajax 获取数据
                    $.ajax({
                        url: "car_series/get_series",
                        type: "POST",
                        dataType: "json",
                        data: {brand_id: brand_id},
                        success: function (data) {
                            // console.log(data);
                            // 修改下拉框的内容
                            $("#c-series_id").html("");
                            for (var i = 0; i < data.length; i++) {
                                $("#c-series_id").append("<option value='" + data[i].id + "'>" + data[i].name + "</option>");
                            }
                        }
                    })
                })
            }
        }
    };
    return Controller;
});
